/**
 * Copyright (c) <2010>, <SUNNY GUPTA>
 * All rights reserved.

 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *   * Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *   * Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
 *     documentation and/or other materials provided with the distribution.
 *   * Neither the name of the <ORGANIZATION> nor the
 *     names of its contributors may be used to endorse or promote products
 *     derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL <SUNNY GUPTA> BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package com.gwtp.investmentadvisor.client.gui.authentication;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.event.dom.client.HasKeyPressHandlers;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HasValue;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;
import com.google.inject.Inject;

import com.gwtp.investmentadvisor.client.resources.AppResources;

/**
 * Composite Widget for Login Screen View.
 * This is made singleton so that the same instance is shared across complex views and it's presenter
 * @author Sunny Gupta
 *
 */
public class LoginScreenView 
    extends Composite implements LoginScreenPresenter.Display {
  /**
   * UiBinder Interface Code.
   */
  private static LoginScreenViewUiView uiBinder = GWT.create(LoginScreenViewUiView.class);

  interface LoginScreenViewUiView extends UiBinder<Widget, LoginScreenView> {
  }
  
  @UiField
  TextBox usernameTextBox;
  
  @UiField
  PasswordTextBox passwordTextBox;
  
  @UiField(provided = true)
  Image usernameErrorImage;
  
  @UiField(provided = true)
  Image passwordErrorImage;
  
  @UiField
  Label usernameErrorLabel;
  
  @UiField
  Label passwordErrorLabel;
  
  @UiField
  Button loginButton;
  
  @UiField
  CheckBox rememberMeCheckBox;
  
  @UiField(provided = true)
  Image messageImage;
  
  @UiField
  Label messageLabel;
  
  @UiField(provided = true)
  AppResources resources;
  
  @Inject
  public LoginScreenView() {
    // Provide resources
    this.resources = AppResources.INSTANCE;
    
    // Initialize error images
    this.usernameErrorImage = new Image(resources.wrongIcon());
    this.passwordErrorImage = new Image(resources.wrongIcon());
    this.messageImage = new Image();
    
    // Initialize widgets
    initWidget(uiBinder.createAndBindUi(this));
    
    // Configure fields
    configureFields();
  }
  
  private void configureFields() {
    // Have remember me checked by default
    this.rememberMeCheckBox.setValue(true);
    
    // Hide error images & labels at start
    clearUsernameError();
    clearPasswordError();
    clearLoginAttemptMessage();
  }
  
  public void clearLoginAttemptMessage() {
    this.messageImage.setVisible(false);
    this.messageLabel.setVisible(false);
  }

  @Override
  public Widget asWidget() {
    // The View extends Composite and hence is a Widget
    return this;
  }

  @Override
  public void clearPasswordError() {
    this.passwordErrorImage.setVisible(false);
    this.passwordErrorLabel.setVisible(false);
  }

  @Override
  public void clearUsernameError() {
    this.usernameErrorImage.setVisible(false);
    this.usernameErrorLabel.setVisible(false);
  }

  @Override
  public HasValue<String> getPasswordText() {
    return this.passwordTextBox;
  }

  @Override
  public HasValue<Boolean> getRememberMeFlag() {
    return this.rememberMeCheckBox;
  }

  @Override
  public HasClickHandlers getSubmitButton() {
    return this.loginButton;
  }

  @Override
  public HasValue<String> getUsernameText() {
    return this.usernameTextBox;
  }

  @Override
  public void raisePasswordError() {
    this.passwordErrorImage.setVisible(true);
    this.passwordErrorLabel.setVisible(true);
  }

  @Override
  public void raiseUsernameError() {
    this.usernameErrorImage.setVisible(true);
    this.usernameErrorLabel.setVisible(true);
  }

  @Override
  public void configureErrorMessages(String usernameErrorMessage,
      String passwordErrorMessage) {
    // Set error messages
    this.usernameErrorLabel.setText(usernameErrorMessage);
    this.passwordErrorLabel.setText(passwordErrorMessage);
  }

  @Override
  public void disableLoginButton() {
    this.loginButton.setEnabled(false);
  }

  @Override
  public void enableLoginButton() {
    this.loginButton.setEnabled(true);
  }

  @Override
  public HasKeyPressHandlers getPasswordKeyPressGenerator() {
    return this.passwordTextBox;
  }

  @Override
  public HasKeyPressHandlers getUsernameKeyPressGenerator() {
    return this.usernameTextBox;
  }

  @Override
  public void configureValues(String username, String password,
      boolean rememberMeFlag) {
    this.usernameTextBox.setValue(username);
    this.passwordTextBox.setValue(password);
    this.rememberMeCheckBox.setValue(rememberMeFlag);
  }

  @Override
  public void setLoginAttemptFailureMessage(String message) {
    this.messageImage.setResource(resources.wrongIcon());
    this.messageLabel.setText(message);
    // Set Visible
    this.messageImage.setVisible(true);
    this.messageLabel.setVisible(true);
  }

  @Override
  public void setLoginAttemptSuccessMessage(String message) {
    this.messageImage.setResource(resources.rightIcon());
    this.messageLabel.setText(message);
    // Set Visible
    this.messageImage.setVisible(true);
    this.messageLabel.setVisible(true);
  }

  @Override
  public void clearFields() {
    this.usernameTextBox.setValue("");
    this.passwordTextBox.setValue("");
  }

  @Override
  public void focusPasswordText() {
    this.passwordTextBox.setFocus(true);
  }

  @Override
  public void focusUsernameText() {
    this.usernameTextBox.setFocus(true);
  }

  @Override
  public void addToSlot(Object slot, Widget content) {
    // TODO Auto-generated method stub
  }

  @Override
  public void removeFromSlot(Object slot, Widget content) {
    // TODO Auto-generated method stub
  }

  @Override
  public void setInSlot(Object slot, Widget content) {
    // TODO Auto-generated method stub
  }
}
